//
//  MOTabBarController.m
//
//  Created by user on 16/3/4.
//  Copyright © 2016年 son. All rights reserved.
//

#import "MOTabBarController.h"
#import "UIImage+Image.h"
#import "UIImageView+WebCache.h"
#import "MOTabBar.h"

#import "MyCollectionViewController.h"
#import "SecondRootViewController.h"
#import "ViewNewsController.h"
#import "FourthRootTableViewController.h"
#import "FirstRootViewController.h"

#import "MONavigationController.h"

#import "MOPublishedViewController.h"


@interface MOTabBarController ()<MOTabBarDelegate>

@property (nonatomic,strong) NSMutableArray *items;

/**
 *  存放表情的数组
 */
@property (nonatomic ,strong) NSMutableArray *rongCloudNameArrs;
/**
 *  本地表情名字数组
 */
@property (nonatomic ,strong) NSMutableArray *myNameArrs;
/**
 *  每个tabbar对应的控制器
 */
//
//@property (nonatomic,strong) MyCollectionViewController *home;
//@property (nonatomic,strong) MOMessageViewController *message;
//@property (nonatomic,strong) MOProfileViewController *profile;



@end

@implementation MOTabBarController

-(NSMutableArray *)myNameArrs {
    if (!_myNameArrs) {
        _myNameArrs = [NSMutableArray array];
        for (id dic in _rongCloudNameArrs) {
            if (![dic isKindOfClass:[NSDictionary class]]) {
                [_myNameArrs addObject:dic];
            }else {
                [_myNameArrs addObject:((NSDictionary *)dic).allKeys.firstObject];
            }
            
        }
//        for (int i = 20; i < 135; i += 21) {
//            NSString *str = @"rc_ic_delete";
//            [_myNameArrs insertObject:str atIndex:i];
//        }
    }
    
    return _myNameArrs;
}
-(NSMutableArray *)rongCloudNameArrs {
    if (!_rongCloudNameArrs) {
        NSString * filePath = [[NSBundle mainBundle] pathForResource:@"questions" ofType:@"plist"];
        //2.根据路径初始化字典对象
        _rongCloudNameArrs = [NSMutableArray arrayWithArray:[NSArray arrayWithContentsOfFile:filePath]];
        
        for (int i = 20; i < 135; i += 21) {
            NSString *str = @"rc_ic_delete";
            [_rongCloudNameArrs insertObject:str atIndex:i];
        }
        
    }
    return _rongCloudNameArrs;
}

-(NSMutableArray *)items{
    if(!_items) {
        
        _items = [NSMutableArray array];
    }
    return _items;
}

// 什么时候调用：程序一启动的时候就会把所有的类加载进内存
// 作用：加载类的时候调用
//+ (void)load
//{
//
//}

// 什么调用：当第一次使用这个类或者子类的时候调用
// 作用：初始化类



- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    
    //创建TabBar的子控制器
    [self setUpAllChildViewController];
    
    
    //自定义tabBar
    [self setUpTabBar];

    /** 保存表情*/
    
    [[NSUserDefaults standardUserDefaults] setObject:self.rongCloudNameArrs forKey:@"rongCloudNameArrs"];
    [[NSUserDefaults standardUserDefaults] setObject:self.myNameArrs forKey:@"myNameArrs"];
}


#pragma mark - 设置tabBar
- (void)setUpTabBar{
    
    //自定义tabbar
    MOTabBar *tabBar = [[MOTabBar alloc] initWithFrame:self.tabBar.bounds];
    
    tabBar.backgroundColor = [UIColor whiteColor];
    // 设置代理
    tabBar.delegate = self;
    
    // 给tabBar传递tabBarItem模型
    tabBar.items = self.items;
    
    [self.tabBar addSubview:tabBar];

}

- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    
}

#pragma mark - 当点击tabBar上的按钮调用  (tabBar的代理方法)
- (void)tabBar:(MOTabBar *)tabBar didClickButton:(NSInteger)index{
    
//    if(index == 0 && self.selectedIndex == index){
//        
//        //点击首页刷新
//        [_home refresh];
//    }
//
    
    //获取对应的点击按钮
    self.selectedIndex = index;
}


//方法抽取 (创建子控制器)
#pragma make - 添加所有的子控件
- (void)setUpAllChildViewController
{
    //创建TabBar的子控制器
    FirstRootViewController *honme = [[FirstRootViewController alloc] init];
    
    [self setUpOneChuildViewController:honme image:[UIImage imageNamed:@"fj"] selectedImaeg:[UIImage imageWithOriginalName:@"fjdj"]  title:@"附近"];

    
   
    SecondRootViewController *message = [[SecondRootViewController alloc] init];
    [self setUpOneChuildViewController:message image:[UIImage imageNamed:@"jb"] selectedImaeg:[UIImage imageWithOriginalName:@"jbdj"]  title:@"结伴"];
    
  
    ViewNewsController *discover = [[ViewNewsController alloc] init];
    [self setUpOneChuildViewController:discover image:[UIImage imageNamed:@"xx"] selectedImaeg:[UIImage imageWithOriginalName:@"xxdj"]   title:@"消息"];
    

    FourthRootTableViewController *profile = [[FourthRootTableViewController alloc] init];
    [self setUpOneChuildViewController:profile image:[UIImage imageNamed:@"wd"] selectedImaeg:[UIImage imageWithOriginalName:@"wddj"]   title:@"我的"];

}
#pragma make - 添加一个子控件
- (void)setUpOneChuildViewController:(UIViewController *)vc image:(UIImage *)imageName selectedImaeg:(UIImage *)seletedImageName title:(NSString *)title{
    
    vc.tabBarItem.image = imageName;
    vc.tabBarItem.selectedImage = seletedImageName;


    vc.title = title;


    vc.tabBarItem.title =title; //设置每个导航条的标题
    
    
    
    //把tabbaritem赋值给成员属性,让自定义的tabbar拿到系统的items
    [self.items addObject:vc.tabBarItem];
    
    //把控制器包装成导航控制器
    MONavigationController *nav = [[MONavigationController alloc] initWithRootViewController:vc];
    
    //添加子控制器
    [self addChildViewController:nav];
    
}

#pragma mark - 点击加号按钮的时候调用
- (void)tabBarDidClickPlusButton:(MOTabBar *)tabBar{
    
    
    MOPublishedViewController *composeVc = [[MOPublishedViewController alloc] init];
    
    //把一个控制器包装成导航控制器
    MONavigationController *nav = [[MONavigationController alloc] initWithRootViewController:composeVc];
    
    //Model出一个控制器
    [self presentViewController:nav animated:YES completion:nil];
    
}

@end
